import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:screen_retriever/screen_retriever.dart';
import 'package:window_manager/window_manager.dart';

import '../utils/logger_manager.dart';
import '../utils/platform_uitl.dart';
import '../utils/preferences_util.dart';

class App {
  static App? _instance;

  factory App() => _instance ?? App._internal();


  App._internal() {
    _instance = this;
  }

  App.dispose() {
  }

  Future<bool> init() async {
    await LoggerManager().init();
    await PreferencesUtil().init();

    if(isPc) {
      Display primaryDisplay = await screenRetriever.getPrimaryDisplay();
      // 获取屏幕可用高度,宽度
      _windowHeight = primaryDisplay.visibleSize?.height ?? 1280;
      _windowWidth = primaryDisplay.visibleSize?.width ?? 800;

      await windowManager.ensureInitialized();
      // 必须加上这一行。
      WindowOptions windowOptions = WindowOptions(
        // size: Size(600, 400), // 设置默认窗口大小
        size: Size(_windowWidth, _windowHeight), // 设置默认窗口大小(占满屏幕)
        minimumSize: const Size(0, 0), // 设置最小窗口大小
        center: true, // 设置窗口居中
        //title: "demo", // 设置窗口标题
      );
      windowManager.waitUntilReadyToShow(windowOptions, () async {
        await windowManager.show();
        await windowManager.focus();
      });
      _titleBarHeight = await windowManager.getTitleBarHeight();
    }
    return true;
  }

  var _windowHeight;
  var _windowWidth;
  var _titleBarHeight;
  final EventBus _eventBus = EventBus();

  double get windowHeight => _windowHeight;
  double get windowWidth => _windowWidth;
  int get titleBarHeight => _titleBarHeight;
  EventBus get eventBus => _eventBus;
}